"""
* 这题存在一些小问题
"""


def output(matrix, n):
    for i in range(n):
        for j in range(n):
            print(matrix[i][j], end="")
        print()
    print('```````````````````````````````')


def resolution1():
    n, m = map(int, input().split())
    matrix = list()
    next_matrix = list()
    # 创建一个n*n的矩阵
    for i in range(n):
        matrix.append(list(input()))
    # 初始化次态矩阵
    next_matrix = matrix
    # print(next_matrix, end="")
    # 感染
    day = 1
    while day != m:

        # test
        output(matrix, n)

        day += 1
        # 开始预测下一天：
        for i in range(n):
            for j in range(n):
                if matrix[i][j] == '@':
                    # 感染上方
                    if (i-1 >= 0) and (matrix[i-1][j] == '.'):
                        next_matrix[i-1][j] = '@'
                        # test
                        print(i, j, 'up')
                        output(next_matrix, n)
                    # 感染下方
                    if (i+1 < n) and (matrix[i+1][j] == '.'):
                        next_matrix[i+1][j] = '@'
                        # test
                        print(i, j, 'down')
                        output(next_matrix, n)
                    # 感染左边
                    if (j-1 >= 0) and (matrix[i][j-1] == '.'):
                        next_matrix[i][j-1] = '@'
                        # test
                        print(i, j, 'left')
                        output(next_matrix, n)
                    # 感染右边
                    if (j+1 < n) and (matrix[i][j+1] == '.'):
                        next_matrix[i][j+1] = '@'
                        # test
                        print(i, j, 'right')
                        output(next_matrix, n)
        # 更新next
        # 这里这个的存在似乎会是的两个matrix存在联系，这种方法有问题，但是我说不来是什么问题！
        matrix = next_matrix.copy()

    # 统计
    sum = 0
    for i in range(n):
        for j in range(n):
            if matrix[i][j] == '@':
                sum += 1
    print(sum)

# 不采用next矩阵的方式，而是直接在原本的matrix上用其他标志标记新感染的人，后续再更新
def resolution2():
    n, m = map(int, input().split())
    matrix = list()
    # 创建一个n*n的矩阵
    for i in range(n):
        matrix.append(list(input()))
    # 初始化次态矩阵
    matrix = matrix
    # print(next_matrix, end="")
    # 感染
    day = 1
    while day != m:

        # test
        output(matrix, n)

        day += 1
        # 开始预测下一天：
        for i in range(n):
            for j in range(n):
                if matrix[i][j] == '@':
                    # 感染上方
                    if (i-1 >= 0) and (matrix[i-1][j] == '.'):
                        matrix[i-1][j] = '&'
                        # test
                        # print(i, j, 'up')
                        # output(matrix, n)
                    # 感染下方
                    if (i+1 < n) and (matrix[i+1][j] == '.'):
                        matrix[i+1][j] = '&'
                        # test
                        # print(i, j, 'down')
                        # output(matrix, n)
                    # 感染左边
                    if (j-1 >= 0) and (matrix[i][j-1] == '.'):
                        matrix[i][j-1] = '&'
                        # test
                        # print(i, j, 'left')
                        # output(matrix, n)
                    # 感染右边
                    if (j+1 < n) and (matrix[i][j+1] == '.'):
                        matrix[i][j+1] = '&'
                        # test
                        # print(i, j, 'right')
                        # output(matrix, n)
        # 更新matrix
        for x in range(n):
            for y in range(n):
                if matrix[x][y] == '&':
                    matrix[x][y] = '@'

    # test
    output(matrix, n)

    # 统计
    sum = 0
    for i in range(n):
        for j in range(n):
            if matrix[i][j] == '@':
                sum += 1
    print(sum)


if __name__ == '__main__':
    # resolution1()
    resolution2()